package com.bauer.framework.autumn.context;

import com.bauer.framework.autumn.factory.BeanFactory;
import com.bauer.framework.autumn.factory.iml.AutowiredBeanFactory;
import com.bauer.framework.autumn.utils.PathUtils;
import javax.servlet.ServletContext;

import java.util.List;

/**
 * @Author ：rui.wei
 * @Date ：Created in 15:04 2018/7/24
 * @Description：
 */
public class AppContext {

    private ServletContext servletContext;

    private BeanFactory beanFactory;

    private final static String SCAN_PATH_NAME = "SCAN_PATH" ;

    public AppContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public ServletContext getServletContext() {
        return servletContext;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public void refresh() {
        initBeanFatory();
    }

    private void initBeanFatory() {
        if (beanFactory == null) {
            beanFactory = new AutowiredBeanFactory();
        }

        String pkgPath = servletContext.getInitParameter(SCAN_PATH_NAME);

        //扫描注解类生成
        String classPath = this.getClass().getResource("/").getPath();
        List<String> listpath = PathUtils.getPkgPath(classPath, pkgPath);
        beanFactory.loadBeanDefination(Thread.currentThread().getContextClassLoader(), listpath);
        //注入
        beanFactory.initAllBean();
    }
}
